/**    
 * @{#} ShareUtils.java Create on 2014-7-2 上午10:26:21    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.evan.common.share;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.evan.common.utils.CommonUtility;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

public class ShareQQUtils {

	private static ShareQQUtils shareUtils;
	private Context context;

	private Tencent mTencent;
	private static String APP_ID;

	private ShareCallBack mOnCompleteCallBack;

	public static void setQQAPPID(String app_id) {
		APP_ID = app_id;
	}

	public ShareQQUtils(Context context) {
		this.context = context;
		mTencent = Tencent.createInstance(APP_ID,
				context.getApplicationContext());
	}

	public static ShareQQUtils getInstance(Context context) {
		if (CommonUtility.isNull(shareUtils)) {
			shareUtils = new ShareQQUtils(context);
		}
		return shareUtils;
	}

	public Tencent getTencent() {
		return mTencent;
	}

	public void setOnCompleteCallBack(ShareCallBack callBack) {
		mOnCompleteCallBack = callBack;
	}

	public void share(String url, Object title, Object summary) {
		final Bundle bundle = new Bundle();
		// 这条分享消息被好友点击后的跳转URL。
		bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
		// 分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_SUMMARY不能全为空，最少必须有一个是有值的。
		bundle.putString(QQShare.SHARE_TO_QQ_TITLE, title.toString());
		// 分享的图片URL
		bundle.putString(
				QQShare.SHARE_TO_QQ_IMAGE_URL,
				"http://e.hiphotos.bdimg.com/album/scrop%3D228%3Bq%3D90/sign=8f460478a41ea8d38e7c335be737027c/d6ca7bcb0a46f21f02b5ed90f4246b600d33ae74.jpg");
		// 分享的消息摘要，最长50个字
		bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary.toString());
		// 手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
		// bundle.putString(Constants.PARAM_APPNAME, "??我在测试");
		// 标识该消息的来源应用，值为应用名称+AppId。
		// bundle.putString(QQShare.share, "星期几");

		if (context instanceof Activity) {
			mTencent.shareToQQ((Activity) context, bundle, new IUiListener() {

				@Override
				public void onCancel() {
				}

				@Override
				public void onComplete(Object response) {
					if (mOnCompleteCallBack != null) {
						mOnCompleteCallBack.callback(response);
					}
				}

				@Override
				public void onError(UiError e) {
					Log.i("MyLog", e.errorMessage + e.errorDetail + "");
				}
			});
		}
	}
}
